//
//  CZPhotoBrowserViewController.swift
//  GZWeibo666
//
//  Created by Apple on 15/12/7.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

class CZPhotoBrowserViewController: UIViewController {
    
    // MARK: - 属性
    /// 大图的URLs
    private var urls: [NSURL]
    
    /// 点击的cell indexPath
    private var indexPath: NSIndexPath
    
    /// cell重用标示
    private let PhotoBrowserReuseIdentifier = "PhotoBrowserReuseIdentifier"
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    init(urls: [NSURL], indexPath: NSIndexPath) {
        self.urls = urls
        self.indexPath = indexPath
        super.init(nibName: nil, bundle: nil)
    }

    
    /// 流水布局
    private var layout = UICollectionViewFlowLayout()

    override func viewDidLoad() {
        super.viewDidLoad()

        prepareUI()
        
        // 设置页码
        pageLabel.text = "\(indexPath.item + 1) / \(urls.count)"
    }
    
    // MARK: - 按钮点击事件
    /// 关闭控制器
    func close() {
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    /// 保存图片
    func save() {
        
    }
    
    private func prepareCollectionView() {
        // 设置collectionView背景颜色
        collectionView.backgroundColor = UIColor.lightGrayColor()
        
        // 设置数据源
        collectionView.dataSource = self
        
        // 设置代理
        collectionView.delegate = self
        
        // 注册cell
        collectionView.registerClass(CZPhotoBrowserCell.self, forCellWithReuseIdentifier: PhotoBrowserReuseIdentifier)
        
        // 设置layout
        layout.itemSize = view.bounds.size
        
        // 水平滚动
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        // 不要弹簧效果
        collectionView.bounces = false
        
        // 分页
        collectionView.pagingEnabled = true
        
        // 不要间距
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
    }
    
    // MARK: - 准备UI
    private func prepareUI() {
        prepareCollectionView()
        
        // 按钮添加点击事件
        closeButton.addTarget(self, action: "close", forControlEvents: UIControlEvents.TouchUpInside)
        saveButton.addTarget(self, action: "save", forControlEvents: UIControlEvents.TouchUpInside)
        
        // 添加控件
        view.addSubview(collectionView)
        view.addSubview(closeButton)
        view.addSubview(saveButton)
        view.addSubview(pageLabel)
        
        // 添加约束
        collectionView.translatesAutoresizingMaskIntoConstraints = false
        closeButton.translatesAutoresizingMaskIntoConstraints = false
        saveButton.translatesAutoresizingMaskIntoConstraints = false
        pageLabel.translatesAutoresizingMaskIntoConstraints = false
        
        let views = ["cv": collectionView, "cb": closeButton, "sb": saveButton]
        // collectionView, 填充父控件
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[cv]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[cv]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))
        
        // 页码
        // 水平方向
        view.addConstraint(NSLayoutConstraint(item: pageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        // 垂直方向
        view.addConstraint(NSLayoutConstraint(item: pageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 25))
        
        // 关闭/保存按钮 水平方向
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-8-[cb(75)]-(>=0)-[sb(75)]-8-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))
        
        // 关闭按钮垂直方向
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[cb(35)]-8-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))
        
        // 保存按钮垂直方向
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[sb(35)]-8-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))
    }
    
    // MARK: - 懒加载控件
    /// collectionView
    private lazy var collectionView: UICollectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: self.layout)
    
    /// 关闭按钮
    private lazy var closeButton: UIButton = UIButton(bkgImageName: "health_button_orange_line", title: "关闭", font: 14)
    
    /// 保存按钮
    private lazy var saveButton: UIButton = UIButton(bkgImageName: "health_button_orange_line", title: "保存", font: 14)
    
    /// 页码
    private lazy var pageLabel = UILabel(color: UIColor.whiteColor(), fontSize: 14)
}

// MARK: - 扩展
extension CZPhotoBrowserViewController: UICollectionViewDataSource, UICollectionViewDelegate {
    
    /// 返回collectionView cell的数量
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return urls.count
    }
    
    /// 返回cell
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(PhotoBrowserReuseIdentifier, forIndexPath: indexPath) as! CZPhotoBrowserCell
        
        cell.backgroundColor = UIColor.randomColor()
        
        // 设置cell的url
        cell.url = urls[indexPath.item]
        
        return cell
    }
}